package com.example.lbstest.controller;

import com.example.lbstest.impl.CardMapperServiceImpl;
import com.example.lbstest.impl.NoticeMapperServiceImpl;
import com.example.lbstest.pojo.Card;
import com.example.lbstest.pojo.Notice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

@Controller
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    NoticeMapperServiceImpl noticeMapperService;

    @RequestMapping("/insertOneNotice")
    @ResponseBody
    public String insertOneNotice(@RequestParam("title")String title,
                                  @RequestParam("content")String content,
                                  @RequestParam("send_name")String send_name,
                                  @RequestParam("send_number")String send_number,
                                  @RequestParam("receive_number")String receive_number,
                                  @RequestParam("label")String label,
                                  @RequestParam("class_number")String class_number,
                                  @RequestParam("class_code")String class_code,
                                  @RequestParam("status")String status){
        Notice notice = new Notice();
        notice.setClass_code(class_code);
        notice.setClass_number(class_number);
        notice.setContent(content);
        notice.setTitle(title);
        notice.setTime(getDate());
        notice.setId(getUId());
        notice.setLabel(label);
        notice.setSend_name(send_name);
        notice.setSend_number(send_number);
        notice.setReceive_number(receive_number);
        notice.setStatus(status);

        int rr = noticeMapperService.insertOneNotice(notice);
        if(rr == 1){
            return "success";
        }
        return "fail";
    }

    @RequestMapping("/selectNoticeListByUser")
    @ResponseBody
    public List<Notice> selectNoticeListByUser(@RequestParam("number")String number){
        List<Notice> noticeList = noticeMapperService.selectNoticeListByUser(number);
        if (noticeList.size() != 0){
            System.out.println("-----查询通知list成功------");
        }
        return noticeList;
    }
    @RequestMapping("/selectNoticeListByUserAndLabel")
    @ResponseBody
    public List<Notice> selectNoticeListByUserAndLabel(@RequestParam("number")String number,
                                                       @RequestParam("label")String label,
                                                       @RequestParam("class_number")String class_number){
        List<Notice> noticeList = noticeMapperService.selectNoticeListByUserAndType(number,label,class_number);
        if (noticeList.size() != 0){
            System.out.println("-----查询通知list成功------");
        }
        return noticeList;
    }

    @RequestMapping("/selectNoticeListByUserAndLeave")
    @ResponseBody
    public List<Notice> selectNoticeListByUserAndLeave(@RequestParam("number")String number,
                                                       @RequestParam("label")String label){
        List<Notice> noticeList = noticeMapperService.selectNoticeListByUserAndLeave(number,label);
        if (noticeList.size() != 0){
            System.out.println("-----查询通知list成功------");
        }
        return noticeList;
    }
    @RequestMapping("/updateNoticeStatus")
    @ResponseBody
    public  String updateNoticeStatus(@RequestParam("id")String id){
        int rr = noticeMapperService.updateNoticeStatus(id);
        if (rr == 1){
            return  "success";
        }else{
            return "fail";
        }
    }

    private String getUId() {
        long time = System.currentTimeMillis();
        String s = String.valueOf(time);
        s=s.substring(3,13);
        long id = Long.valueOf(s);
        return String.valueOf(id);
    }
    //获取当前时间
    private String getDate() {
        Date dNow = new Date( );
        SimpleDateFormat ft =
                new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        return ft.format(dNow);

    }

}
